#  This file is part of codeogre
#  Copyright (C) 2009 Johan Jordaan (www.johanjordaan.co.za)
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
sys.path.append('..\..')
import unittest

from codeogre.fsm import *

class FSMExecuteTestCases(unittest.TestCase):

    def setUp(self):
      pass

    def test_enter_function(self):
      ''' Test Enter Function '''
      self.count = 0
      def reset():
        self.count = 0
      def count():
        self.count = self.count + 1
      
      fsm = FSM()
      fsm.start_state.enter_function = reset
      s = FSMState(fsm,accepting=True,enter_function=count)
      t1 = FSMTransition(source=fsm.start_state,destination=s,transition_function=lambda input,input_index:input[input_index]=='a')
      tloop = FSMTransition(source=s,destination=s,transition_function=lambda input,input_index:input[input_index]=='a')
      
      self.assertEqual(True,fsm.accepts('aaaabb'))
      self.assertEqual(['aaaa','aaa','aa','a' ],fsm.accepted_inputs)      
      fsm.execute('aaaabb')
      self.assertEqual(4,self.count)
      fsm.execute('aaaabb')
      self.assertEqual(4,self.count)
      
    def test_exit_function(self):
      ''' Test Exit Function '''
      self.count = 0
      def reset():
        self.count = 0
      def count():
        self.count = self.count + 1
      
      fsm = FSM()
      fsm.start_state.accepting = True
      fsm.start_state.enter_function = reset
      fsm.start_state.exit_function = count
      tloop = FSMTransition(source=fsm.start_state,destination=fsm.start_state,transition_function=lambda input,input_index:input[input_index]=='a')
      
      self.assertEqual(True,fsm.accepts('aaaabb'))
      self.assertEqual(['aaaa','aaa','aa','a','' ],fsm.accepted_inputs)      
      fsm.execute('aaaabb')
      self.assertEqual(4,self.count)
      fsm.execute('aaaabb')
      self.assertEqual(4,self.count)
      
      
      

suite = unittest.makeSuite(FSMExecuteTestCases)
          
if __name__ == '__main__':
  unittest.main()
